package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 31803
 * @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
 * @createDate 2025-06-20 15:27:20
 */
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
        implements TbBrandService {

    @Autowired
    TbBrandMapper tbBrandMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public R getBrandList(PageInfoVo pageInfoVo) {
        //1构造分页参数
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        //2分页查询
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();
        //品牌名称模糊查询
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbBrand::getBrandName,pageInfoVo.getKeyWord());
        }
        //根据创建时间排序
        wrapper.lambda().orderByDesc(TbBrand::getCreateTime);

        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);

        return R.ok().setData(tbBrandPage);
    }

    @Override
    public R AddBrand(TbBrand tbBrand) {
        //1判断品牌是否重名
        TbBrand tbBrand1 = tbBrandMapper.selectOne(new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName()));
        if (tbBrand1 != null) {
            return R.erorr().setMsg("品牌已经存在");
        }
        //2添加
        String token = request.getHeader("token");
        if (token == null) {
            return R.erorr().setMsg("用户未登录");
        }
        String userName = TokenUtils.getUserName(token);

        tbBrand.setCreateBy(userName);
        tbBrand.setCreateTime(new Date());
        tbBrandMapper.insert(tbBrand);
        return R.ok();
    }

    @Override
    public R updataStatus(TbBrand tbBrand) {
        //查看品牌信息
        TbBrand tbBrand1 = tbBrandMapper.selectById(tbBrand);
        if(tbBrand1==null){
            return R.erorr().setMsg("品牌不存在");
        }
        //修改状态
        tbBrand1.setBrandStatus(tbBrand.getBrandStatus());
        String token = request.getHeader("token");
        if(token==null){
            return R.erorr().setMsg("token不存在请登录");
        }

        String userName = TokenUtils.getUserName(token);
        tbBrand1.setUpdateBy(userName);
        tbBrand1.setCreateTime(new Date());
        tbBrandMapper.updateById(tbBrand);
        return R.ok();
    }

    @Override
    public R deleteBrand(Integer brandId) {
        //1判断是否存在
        TbBrand tbBrand = tbBrandMapper.selectById(brandId);
        if(tbBrand==null){
            return R.erorr().setMsg("品牌不存在");
        }
        //2删除品牌
        tbBrandMapper.deleteById(tbBrand.getBrandId());

        return R.ok();
    }

    @Override
    public R DeleteBacth(List<TbBrand> tbBrands) {
        //判断数据是否为空
        if(tbBrands==null||tbBrands.size()==0){
            return R.erorr().setMsg("请选择数据");
        }
        //循环列表删除
//        for (TbBrand tbBrand : tbBrands) {
//            tbBrandMapper.deleteById(tbBrand.getBrandId());
//        }
        List<Integer> ids = tbBrands.stream().map(item -> item.getBrandId()).collect(Collectors.toList());
        tbBrandMapper.deleteBatchIds(ids);
        return R.ok();
    }

    @Override
    public R BrandList() {
        List<TbBrand> tbBrands = tbBrandMapper.selectList(null);

        return R.ok().setData(tbBrands);
    }
}




